<?php

class StatusManager extends SManager
{
    public function from_friends_of($user_id, $limit = 20)
    {
        return $this->by_sql(
            "SELECT statuses.*, favorites.id AS favorite_id FROM statuses
            LEFT OUTER JOIN users ON users.id = statuses.sender_id 
            LEFT OUTER JOIN friendships ON statuses.sender_id = friendships.friend_id
            LEFT OUTER JOIN favorites ON (favorites.user_id = friendships.user_id AND favorites.status_id = statuses.id)
            WHERE friendships.user_id = '{$user_id}' OR statuses.sender_id = '{$user_id}'
            ORDER BY timestamp DESC LIMIT {$limit}"
        );
    }
}

class Status extends SActiveRecord
{
    public static $objects;
    public static $relationships = array(
        'sender' => array('assoc_type' => 'belongs_to', 'foreign_key' => 'sender_id', 'class_name' => 'User')
    );
    
    public function validate()
    {
        $this->validate_presence_of(array('text', 'sender_id'));
    }
    
    public function serializable_form()
    {
        $obj = parent::serializable_form(array('include' => array('sender')));
        if (array_key_exists('favorite_id', $this->values)) {
            $obj->favorite_id = $this->values['favorite_id'];
        }
        return $obj;
    }
}

?>